package com.admin.kotlintest.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class FileUtil {

	/**
	 * 根据uri获取文件路径
	 * @param context
	 * @param uri
	 * @return
	 */
	public static String getFilepathForUrl(Context context,Uri uri){
		try {
			String[] proj = {MediaStore.Files.FileColumns.DATA};
			ContentResolver contentResolver = context.getContentResolver();
			Cursor cursor = contentResolver.query(uri, proj, null, null, null);
			int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
			cursor.moveToFirst();
			String filePath = cursor.getString(pathIndex);
			cursor.close();
			return filePath;
		}catch (Exception e){
			return uri.getPath();
		}

	}


	//获取文件名
	public static String getFileNameForUrl(String url){
		String[] str=url.split("\\/");
		return str[str.length-1];
	}



}
